﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace UPnP
{
    public class Device
    {
        /*
         * HTTP/1.1 200 OK 
  CACHE-CONTROL: max-age = seconds until advertisement expires 
 DATE: when response was generated 
 EXT: 
 LOCATION: URL for UPnP description for root device 
 SERVER: OS/version UPnP/1.1 product/version 
  ST: search target 
  USN: composite identifier for the advertisement 
  BOOTID.UPNP.ORG: number increased each time device sends an initial announce or an update 
message 
  CONFIGID.UPNP.ORG: number used for caching description information 
  SEARCHPORT.UPNP.ORG: number identifies port on which device responds to unicast M-SEARCH 
         * 
         */
        public string ToSSDPResponse()
        {
            string myMsg = "HTTP/1.1 200 OK\r\n";
            myMsg += "cache-control: 900\r\n";
            myMsg += "date: " + DateTime.Now.ToLongDateString() + "\r\n";
            myMsg += "Server: " + Environment.OSVersion.Platform.ToString() + "/" + Environment.OSVersion.Version.ToString() + " UPnP/1.1 DaveUpnp/1.0\r\n";
            myMsg += "location: " + Location + "\r\n";
            myMsg += "DeviceName.davux.com: " + DeviceName + "\r\n";
            myMsg += "URL.davux.com: " + URL + "\r\n";
            myMsg += "usn: " + USN + "\r\n\r\n";
            return myMsg;
        }

        public Device()
        {

        }

        public Device(string locationXML)
        {
            if (locationXML == "")
            {
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(locationXML);

            FriendlyName = GetElement(doc, "friendlyName");
            USN = GetElement(doc, "USN");
            DeviceType = GetElement(doc, "deviceType");
            Manufacturer = GetElement(doc, "manufacturer");
            ManufacturerURL = GetElement(doc, "ManufacturerURL");
            ModelDescription = GetElement(doc, "modelDescription");
            ModelNumber = GetElement(doc, "modelNumber");
            ModelName = GetElement(doc, "modelName");
            ModelURL = GetElement(doc, "modelURL");
            UDN = GetElement(doc, "UDN");
            UPC = GetElement(doc, "UPC");
            PresentationURL = GetElement(doc, "PresentationURL");
        }

        private static string GetElement(XmlDocument doc, string element)
        {
            XmlNodeList xnl = doc.GetElementsByTagName(element);
            if (xnl.Count > 0)
            {
                return xnl[0].InnerText;
            }
            return "";
        }

        public string DeviceName { get; set; }

        public string USN { get; set; }
        public string FriendlyName { get; set; }
        public string ManufacturerURL { get; set; }
        public string Manufacturer { get; set; }
        public string ModelDescription { get; set; }
        public string ModelNumber { get; set; }
        public string ModelName { get; set; }
        public string ModelURL { get; set; }
        public string UDN { get; set; }
        public string UPC { get; set; }
        public string DeviceType { get; set; }
        public string PresentationURL { get; set; }
        public string Location { get; set; }
        public string Host { get; set; }
        public string URL { get; set; }
        public object Tag { get; set; }

        public List<Service> ServiceList = new List<Service>();
        public List<Icon> IconList = new List<Icon>();

    }
}
